﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Lumos.Areas.Reports.Controllers
{
    [Authorize]
    public class DailyWorklogController : Controller
    {
        // GET: Reports/DailyWorklog
        [HttpGet]
        public ActionResult Index(string year, string month)
        {
            string worklogFullMonth;
            int worklogDaysInMonth;
            bool invalidEntries = false;
             DateTime worklogRange =new DateTime();
            
            //if asked, try to parse
             if ((month != null && year != null))
            {
                string firstDate = year + "-" + month + "-" + "01";
                if (DateTime.TryParse(firstDate, out worklogRange))
                {
                    //valid date, so continue
                    invalidEntries = false;
                }
                else
                {
                    ViewData.ModelState.AddModelError("INVALID_DATERANGE", "Invalid date range selected. Defaulting to current month.");
                    invalidEntries = true;
                }
            }
            //if not asked, assign default
             if ((month == null && year == null) || invalidEntries == true)
            {
                worklogRange = DateTime.Now;
            }

            string worklogYear = worklogRange.Year.ToString();
            string worklogMonth = worklogRange.Month.ToString();
            worklogFullMonth = worklogRange.ToString("MMMM");
            worklogDaysInMonth = DateTime.DaysInMonth(Int16.Parse(worklogYear), Int16.Parse(worklogMonth));

            //get data from database

            //send values to view
            ViewBag.worklogDaysInMonth = worklogDaysInMonth;
            ViewBag.worklogRangeTitle = worklogFullMonth + " " + worklogYear;
            ViewBag.worklogRange = worklogRange;
            
            return View();
        }
        [HttpPost]
        public ActionResult Index(string save)
        {
            return View();
        }
    }
}